import Vue from 'vue'
import App from './App.vue'
import router from './router'
import store from './store'
import ElementUI, { Message } from 'element-ui'
import 'element-ui/lib/theme-chalk/index.css'
import axios from 'axios'
import '@/assets/global.less'

// 富文本编辑器
import VueQuillEditor from 'vue-quill-editor'

// require styles
import 'quill/dist/quill.core.css'
import 'quill/dist/quill.snow.css'
import 'quill/dist/quill.bubble.css'

Vue.use(VueQuillEditor /* { default global options } */)

Vue.prototype.$axios = axios
axios.defaults.baseURL = 'http://www.liulongbin.top:3008'
Vue.config.productionTip = false
Vue.use(ElementUI)
// 添加请求拦截器
axios.interceptors.request.use(function (config) {
  // 在发送请求之前做些什么
  // console.log(config)
  // console.log(store)
  config.headers.Authorization = store.state.User.token
  return config
}, function (error) {
  // 对请求错误做些什么
  return Promise.reject(error)
})
let flag = true
axios.interceptors.response.use(function (response) {
  // 对响应数据做点什么
  return response
}, function (error) {
  console.log(error)
  // 对响应错误做点什么
  if (error.response.status === 401 && flag === true) {
    flag = false
    store.commit('User/setToken', '')
    router.push('/login')
    Message.error('token失效请登录')
    setTimeout(() => {
      flag = true
    }, 3000)
  }
  return Promise.reject(error)
})

new Vue({
  router,
  store,
  render: h => h(App)
}).$mount('#app')
